// 我的项目日志添加记录
function getWorksList(CertusFormAjax) {
    var myplogColumns = [{
        id: 'logTime',
        title: '日期',
        type: 'date',
        width: 150,
        format: 'yyyy-MM-dd',
        otype: 'time_stamp_ms',
        columnClass: 'text-center',
    }, {
        id: 'hourNum',
        title: '小时数',
        type: 'string',
        width: 75,
        columnClass: 'text-center',
    }, {
        id: 'userName',
        title: '申请人',
        type: 'string',
        width: 75,
        columnClass: 'text-center',
    }, {
        id: 'proName',
        title: '项目名称',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
    }
    // , {
    //     id: 'comments',
    //     title: '事由',
    //     type: 'string',
    //     columnClass: 'text-left',
    // }
    , {
        id: 'operation',
        title: '操作',
        width: 70,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            var ndate = new Date();
            var d = ndate.getDate();
            var m = ndate.getMonth()+1;
            var y = ndate.getFullYear();
            var td = new Date(y+"-"+m+"-02");
            var ld = new Date(record.logTime);
            //每月2号之前的数据不能删除
            // if(ld>td){
                content += '<a class="btn btn-xs btn-danger" onclick="delplog(\''+record.id+'\')"><i class="fa fa-trash-o"></i>  删除</a>';
            // }

            return content;
        }
    }];
    $("#myplogContainer").html("");
    $("#myplogToolBarContainer").html("");
    CertusFormAjax.ajax("findOkrWors",{},function (response) {
        var myplogOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '日志列表',
            datas: response.data,
            columns: myplogColumns,
            gridContainer: 'myplogContainer',
            toolbarContainer: 'myplogToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 60]
        };
        var myplogGrid = $.fn.dlshouwen.grid.init(myplogOption);
        myplogGrid.load();
    });
}

//比较两个时间  time1，time2均为日期类型  ，time1为前时间，time2为后时间 m为相差月份
function completeDate(time1 , time2 , m)
{
    var diffyear = time2.getFullYear() - time1.getFullYear() ;
    var diffmonth = diffyear * 12 + time2.getMonth() - time1.getMonth() ;
    if(diffmonth < 0 ){
        return false ;
    }

    var diffDay = time2.getDate() - time1.getDate() ;

    if(diffmonth < m || (diffmonth == m && diffDay <= 0)){

        if(diffmonth == m && diffDay == 0){
            var timeA = time1.getHours()*3600+60*time1.getMinutes()+time1.getSeconds();
            var timeB = time2.getHours()*3600+60*time2.getMinutes()+time2.getSeconds();
            if(timeB-timeA > 0){
                return false;
            }
        }
        return true ;
    }
    return false ;
}


var $CertusFormAjax;
function addOkrWorks(CertusFormAjax) {
    $CertusFormAjax = CertusFormAjax;
    getWorksList(CertusFormAjax);

    CertusFormAjax.ajax("getCountType",{},function (response) {
        if (response.data == 0){
            $("#datetime-1").attr("disabled",true);
            $(".tj").attr("disabled",true);
            layer.msg("您未有权限进行工时填写，请联系项目负责人");
            return;
        }
    });

    var whs = 0;
    this.searchTime = function () {
        var planetime = $("#planetime").val();
        var planeDate = new Date(planetime);
        var ndate = new Date();
        var d = ndate.getDate();
        var m = ndate.getMonth()+1;
        var y = ndate.getFullYear();

        var timeArr = planetime.split("-");
        if (d>=2){
            //前N年
            if (y-timeArr[0]>0){
                layer.msg("每月2号不能提交上一个月及上个月之前的工时！");
                $(".tj").attr("disabled",true);
                return;
            }else if (y-timeArr[0]==0){
                //同一年
                if (m!=1){
                    if (m - timeArr[1]>0){
                        layer.msg("每月2号不能提交上一个月及上个月之前的工时！");
                        $(".tj").attr("disabled",true);
                        return;
                    }
                }
            }
        }else{
            //每月1号
            var isTrue = completeDate(ndate,planeDate,1);
            if (isTrue){
                layer.msg("提交日期不能超过一个月！");
                $(".tj").attr("disabled",true);
                return;
            }
        }
        CertusFormAjax.ajax("getWorkHours",{"logTime":planetime},function (response) {
            whs = 24-response.data;
            $("#hourNum").attr("placeholder","剩余工时:"+whs);
        });
        $(".tj").attr("disabled",false);
    };

    CertusFormAjax.ajax("findProList1",{},function(response){
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].proName;
            arr.id = data[i].id;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#proName').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });

    $(".tj").click(function () {
        var proId = $("#proId").val();
        if (proId==""){
          layer.msg("请选择所属项目！");
          return;
        }

        var planetime = $("#datetime-1").val();
        if (planetime==""){
            layer.msg("请输入周期结束时间！");
            return;
        }
        var workList = [];
        var isTg = true;
        //记录有多少为0 的项目工时
        var num = 0;
        var lt = "";
        $("#content").find("div").find("tr").each(function (k,n) {
            var arr = {};
            arr.logTime = $($($(n).find("td")[0]).find("input")).val();
            arr.works = $($($(n).find("td")[1]).find("select")).val();
            var sysWorks = $($($(n).find("td")[2]).find("span")).html();
            if (Number(arr.works)>Number(sysWorks)){
                isTg = false;
                lt = arr.logTime;
                return isTg;
            }
            if (Number(arr.works)==0){
                num++;
            }
            workList.push(arr);
        });
        if (!isTg){
            layer.msg(lt+"不能大于剩余工作时间");
            return;
        }
        if (num==7){
            layer.msg("至少需要一条大于0的项目工时才能提交！");
            return;
        }
        // var hourNum = $("#hourNum").val();
        // if (hourNum>whs){
        //     layer.msg("剩余工时不足"+whs);
        //     return;
        // }
        // if (hourNum==""||hourNum==0){
        //     layer.msg("小时数不能为空或者0！");
        //     return;
        // }
        // var comments = $("#comments").val();
        var comments = "";
        // if (comments==""){
        //     layer.msg("事由不能为空！");
        //     return;
        // }
        var data = {};
        data.proId = proId;
        data.proName = $("#proName").val();
        data.comments = comments;
        data.workList = workList;
        // CertusFormAjax.ajax('isHasWorkLog',{"logTime":planetime,"proId":proId},function (result) {
        //     if (result.data == false){
                CertusFormAjax.ajax("addOkrWorks",{"param":JSON.stringify(data)},function () {
                    layer.msg("添加成功！");
                    getWorksList(CertusFormAjax);
                    $("#content").hide();
                    reset();
                    $("#datetime").val("");
                    $("#datetime-1").val("");
                    // searchTime();
                });
        //     }else{
        //         layer.msg(planetime+"已经填写过工时，如需修改，请删除后填写！");
        //         return;
        //     }
        // });


    });

    this.delplog =function (id) {
        layer.confirm('确认删除此数据？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            CertusFormAjax.ajax("delOkrWorks",{"id":id},function () {
                layer.msg('删除成功');
                getWorksList(CertusFormAjax);
            });
        });
    }
}